package com.handwriting.backend.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;

public class TokenUtils {
    public static final long expire = 604800;
    private static final String secret = "JlJlYkgyZm5KOUw4YSNWY2xyNm1YeDErZjdvY0dYVzZjUD1EVj14OVo4SSN5KmRFNWptaE14R3dnWlctbXZUXz12IUBxOUNQZU80THdpIzkqTmZHc1d2N1N6K1puSE4mQHUyVGckX2hPN3lseUxsVCRhSyNSdHE2SzF2R2ZMaC04SUlAVHpabFc0NTYmQGhYbjlKPU9SNzlDNy1TRyQ1byF4cEx4Y1VEK2d4aUdydDQydk1GNnU9WDkjMVJLWVBUbVpHdV5CeUpYYzBKZmRleW9CVVc4WGxDYTJQejU0Sjc1SnJDdWY4JG4tU0dFJjgtKnprQFA1LWQqVmxzaVUjOQ==";

    public static String generateToken(String username) {
        SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secret));
        Date now = new Date();
        Date expiration = new Date(now.getTime() + 1000 * expire);
        return Jwts.builder().subject(username).issuedAt(now).expiration(expiration).signWith(key).compact();
    }

    public static Claims getClaimsByToken(String token) {
        SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secret));
        return Jwts.parser().verifyWith(key).build().parseSignedClaims(token).getPayload();
    }
}
